package ui;

import cmm.*;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

/**
 * @author masker
 * @date 2017/12/4 下午8:24
 */
public class DebugThread extends Thread {

    private Editor editor;

    public DebugThread(Editor editor){
        this.editor = editor;
    }

    public void run() {
        CharStream charStream = CharStreams.fromString(editor.editPane.getText());
        CMMLexer lexer = new CMMLexer(charStream);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        CMMParser parser = new CMMParser(tokens);
        ParseTree tree = parser.program();
        //检验错误
        MyVisitor v=new MyVisitor();
        v.visit(tree);
        if(!MyVisitor.hasError)
        {
            // 中间代码
            MidCodeGenerator midCode = new MidCodeGenerator();
            midCode.visit(tree);
            editor.refreshMidCode(midCode.getMidCodes());
            // 代码执行
            MidCodeDebug debug = new MidCodeDebug(editor, midCode.getMidCodes());
            debug.debugMidCode();

            System.out.println("Process finished with exit code 0");
        }

    }

}
